# non-vectorized log()
log_scalar <- function(x) {
tryCatch({
if (length(x) != 1) {
stop("Input must be a single value.")
}
return(log(x))
}, error = function(e) {
cat("Caught an error:", e$message, "\n")
return(NA)
})
}pushoverr
Tristan Contant, STAT-600, 2025-09-11
Motivation
With the advent of big data and great computational power, it has become common to have scripts that take a long time to run, notably in Bayesian computation. When running long scripts, it is helpful to periodically get updates.
- errors: Is my script still running? If not, why did it stop?
- progress: How far along is the script? How much longer will it take?
When working locally in Rstudio, these updates are easily obtained. Error detection can be addressed by printing helpful messages within functions that explain errors. For example, we may want to check the type of objects given to a function. In the following example, we only accept scalar values, not vectors.
We compare the two functions.
log(c(1, 2))[1] 0.0000000 0.6931472
log_scalar(c(1, 2))Caught an error: Input must be a single value.
[1] NA
Tracking progress can be addressed using the progress package which will display handy metrics. For example, you can get a progress bar output to your console that looks like
Progress [==========>----------------------] 32% ETA: 7s
If working remotely, you may not have access to an integrated development environment (IDE) such as RStudio. For example, you may be running scripts on a high-performance computing system such as Alpine. In this situation, creating log files to store errors and progress can be a way to to get updates.
dir.create("logs", recursive = TRUE, showWarnings = FALSE)
sink(paste0("logs/log_", Sys.Date(), ".txt"), split = TRUE)All of the above is easily implemented, but it it requires that you consistently pull up Rstudio or log into servers. You have to always have access to your computer to check errors/progress, along with signing into VPNs if your server has such securities.
To avoid this hassle, pushoverr is a package that can send push notifications to your phone, smart watch, or web browser. You can see how your R script is performing on the go. Catch errors in your script as they come up, rather than waiting all day for your script to finish only to realize that it encountered an error at the beginning. See your script’s progress in real time.
Setup
To get started, we first need to create a pushover account at https://pushover.net/signup. A free 30 day trial is available. After that, a one-time payment of $5.99 is required. You can send up to 10,000 notifications a month.
Once you have created an account, you will have a user key which will uniquely identify you in the pushover system.
Before coding in R, we must add our devices (the computers or servers that will be creating push notifications) and create an API token. This is straightforward within the website.
In this tutorial, we will assume you want push notifications sent to your phone. Download the pushover app and log in with the pushover account you just created.
Now, we are ready to begin coding. Install and load the pushoverr package.
instal.packages("pushoverr")
library(pushoverr)Two lines are necessary to link your R script to your pushover account:
set_pushover_user(user = "...")
set_pushover_app(token = "...")Include this at the beginning of your .R files. You can find your API token on the pushover website. If you want to share your code without sharing your user key and API token, save these strings in separate files.
Usage
To send a push notification, call
pushover(message = "Hello world.")where you can set the message to whatever string you would like.